var IP = "192.168.105.79";


var ws_action = new ReconnectingWebSocket("ws://" + IP + ":1884/ws/actions");
ws_action.onmessage = function (e) {

    var obj = JSON.parse(e.data);
    if (obj.action == 'sendCameraBImage') {
        sendCameraBImage();
    }
    else if (obj.action == 'mes_response') {
        
    }    
};

// 新增函数：发送cameraB的图片
sendCameraBImage = function () {
    if (!cameraBRenderer) return;

    // 获取canvas元素
    const canvas = cameraBRenderer.domElement;

    // 转换为Base64格式
    const imageData = canvas.toDataURL('image/jpeg');
    console.debug(imageData);
    // 准备发送的数据
    const data = {
        image: imageData,
        timestamp: new Date().toISOString()
    };

    $.ajax({
        url: 'http://192.168.105.79:1884/post_image',
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify(data),
        dataType: 'json',
        success: function(response) {
            console.log('Success:', response);
            $('#image').html(response.type+"<br/>"+response.barcode);
        },
        error: function(error) {
            console.error('Error:', error.responseText || error.statusText);
        }
    });    

} 